from . import api_blueprint
from flask import request, json
from app import db
from app.model.user import User
from app.model.admin import Admin
from app.model.teacher import Teacher
from app.model.student import Student


# 获取所有用户
@api_blueprint.route('/user/all', methods=['GET'])
def get_all_users():
    rows = User.query.order_by(User.User_identity).all()
    return json.dumps(rows, default=User.to_json)


# 按条件查询用户
@api_blueprint.route('/user', methods=['GET'])
def get_users_by_condition():
    user_type = request.values.get('type')
    user_id = request.values.get('User_id')
    if user_type == '-1' or user_type == '':
        user_type = None
    if user_id == '':
        user_id = None
    if user_type is None and user_id is None:
        rows = User.query.order_by(User.User_identity).all()
    elif user_type is None:
        rows = User.query.filter(User.User_id.like('%' + user_id + '%')).order_by(User.User_identity).all()
    elif user_id is None:
        rows = User.query.filter(User.User_identity == user_type).order_by(User.User_identity).all()
    else:
        rows = User.query.filter(User.User_id.like('%' + user_id + '%')).filter(
            User.User_identity == user_type).order_by(
            User.User_identity).all()
    return json.dumps(rows, default=User.to_json)


# 添加用户
@api_blueprint.route('/user', methods=['POST'])
def add_user():
    log_id = request.values.get('log_id')
    user_pwd = request.values.get('user_pwd')
    user_id = request.values.get('user_id')
    user_name = request.values.get('user_name')
    user_type = request.values.get('user_type')

    if log_id is None or user_pwd is None or user_id is None or user_name is None or user_type is None:
        return json.dumps({
            'status': 'error',
            'msg': 'param is missing'
        }), 400

    user = User()
    user.User_id = log_id
    user.User_pwd = user_pwd
    user.User_identity = int(user_type)
    db.session.add(user)
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    if user_type == '0':
        admin = Admin()
        admin.Admin_id = user_id
        admin.Admin_log_id = log_id
        admin.Admin_name = user_name
        db.session.add(admin)
    elif user_type == '1':
        teacher = Teacher()
        teacher.Tea_id = user_id
        teacher.Tea_log_id = log_id
        teacher.Tea_name = user_name
        db.session.add(teacher)
    elif user_type == '2':
        student = Student()
        student.Stu_id = user_id
        student.Stu_log_id = log_id
        student.Stu_name = user_name
        db.session.add(student)
    else:
        return json.dumps({
            'status': 'error',
            'msg': 'invalid user_type'
        }), 400
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    return json.dumps({
        'status': 'success'
    })


@api_blueprint.route('/user', methods=['DELETE'])
def del_user():
    log_id = request.values.get('log_id')
    if log_id is None:
        return json.dumps({
            'status': 'error',
            'msg': 'log_id is missing'
        }), 400
    row = User.query.filter(User.User_id == log_id).first()
    if row is None:
        return json.dumps({
            'status': 'error',
            'msg': 'target user is not found'
        }), 404
    if row.User_identity == 0:
        admin = Admin.query.filter(Admin.Admin_log_id == row.User_id).first()
        db.session.delete(admin)
    elif row.User_identity == 1:
        teacher = Teacher.query.filter(Teacher.Tea_log_id == row.User_id).first()
        db.session.delete(teacher)
    elif row.User_identity == 2:
        student = Student.query.filter(Student.Stu_log_id == row.User_id).first()
        db.session.delete(student)
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    db.session.delete(row)
    try:
        db.session.commit()
    except Exception as e:
        print(e)
        return json.dumps({
            'status': 'error',
            'msg': 'db operation error'
        }), 500
    return json.dumps({
        'status': 'success'
    })
